using Gadgeteer.Interfaces.Fakes.Framework;
using System;
using System.Threading;

namespace Gadgeteer.Interfaces.Fakes
{
    /// <summary>
    /// Fake camera, uses pictures from <see cref="PictureBuilder"/> as sample pictures.
    /// </summary>
    public class FakeCamera : DelayedAction, ICamera
    {
        private static int unNamedButtons = 0;
        private PictureBuilder pictureBuilder = new PictureBuilder();

        /// <summary>
        /// Create a fake instance of <see cref="ICamera"/>.
        /// </summary>
        public FakeCamera()
            : base("camera" + ++unNamedButtons)
        {
        }

        /// <summary>
        /// Take a picture, the pictures is send to <see cref="PictureCaptured"/>
        /// after 100 miliseconds.
        /// </summary>
        public void TakePicture()
        {
            ExecuteAsDelayed(TakeNextPicture, 
                new TimeSpan(0, 0, 0, 0, 100)
            );
        }

        private void TakeNextPicture()
        {
            var picture = pictureBuilder.NextPicture();
            OnPictureCaptured(picture);
        }

        private void OnPictureCaptured(Picture picture)
        {
            var ev = PictureCaptured;
            if (ev != null)
                ev(this, picture);
        }

        /// <summary>
        /// <see cref="ICamera.PictureCaptured"/>
        /// </summary>
        public event PictureCapturedEventHandler PictureCaptured;
    }
}
